package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketComment;
import com.cskaoyan.market.service.MarketCommentService;
import com.cskaoyan.market.service.MarketCommentServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

import java.util.List;

@WebServlet("/admin/comment/*")
public class AdminCommentController extends HttpServlet {
    private MarketCommentService marketCommentService =new MarketCommentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/comment/", "");
        if("list".equals(op)){
            list(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/comment/", "");
        if("delete".equals(op)){
            delete(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader requestBody = req.getReader();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketComment marketComment = objectMapper.readValue(requestBody, MarketComment.class);
        marketCommentService.delete(marketComment);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam =req.getParameter("page");
        String limitParam=req.getParameter("limit");
        String userId=req.getParameter("userId");
        String valueId=req.getParameter("valueId");
        String sort =req.getParameter("sort");
        String order = req.getParameter("order");
        Integer page=null;
        Integer limit=null;
        //如果输入的页码和条数参数不是数字
        try {
            page=Integer.parseInt(pageParam);
            limit=Integer.parseInt(limitParam);
        }catch (Exception e){
            //输出参数错误
            Object o = ResponseUtil.badArgument();
            //将json文本转换为字符串
            String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
            resp.getWriter().println(s);
        }
        //处理业务逻辑
        List<MarketComment> marketCommentList = marketCommentService.list(page, limit, userId, valueId, sort, order);
        //返回响应
        Object o = ResponseUtil.okList(marketCommentList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
    }
}

